﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Job.Web.Framework;
using Job.Entity;
using Job.Logic.Company;

namespace Job.Web.userControls.common
{
    public partial class CompanySearcherInfo : BaseCtrl
    {
        private int SearcherId 
        {
            get { return myR["id"].IntValue; }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (LoadData())
                {
                    SetAccess();
                }
                else
                {
                    this.btnSave.Enabled = false;
                    this.btnSearch.Enabled = false;
                }
            }
        }

        private void ShowMessage(string message)
        {
            this.message.InnerHtml = "<font color='red'>" + message + "</font>";
            this.message.Visible = true;
            this.message.Height = "30px";
            this.message.VAlign = "middle";
        }

        private void SetAccess()
        {
            MySession session = new MySession();
            bool haveAccess = (session.GetAdminUserFromSession() != null) ;
            this.adminTr0.Visible = haveAccess;
            this.adminTr1.Visible = haveAccess;
            this.adminTr2.Visible = haveAccess;
            this.adminTr3.Visible = haveAccess;

            if ( !haveAccess )
            {
                this.ddlResumeState.SelectedValue = "1";
                this.ddlPersonSenior.SelectedValue = "2";
                this.ddlPersonClass.SelectedValue = "0";
                this.txtLoginCode.Text = "";
            }
        }

        private bool LoadData()
        {
            JobCompanySearcher searcher;
            if (SearcherId > 0)
            {
                searcher = new SearcherLogic().GetSearcher(SearcherId);
                if (null == searcher)
                {
                    ShowMessage("无法查询到搜索器定义的内容！");
                    return false;
                }
                else
                {
                    MySession session = new MySession();
                    SessionCompany company = session.GetCompanyFromSession();
                    if (!searcher.CreatorCompany.Equals(company.CompanyId)
                        || !searcher.CreatorLogincode.Equals(company.LoginCode))
                    {
                        ShowMessage("您无权管理指定的搜索器！");
                    }
                }
            }
            else
            {
                searcher = new JobCompanySearcher();
            }
            this.ShowInput(searcher);
            return true;
        }

        private void ShowInput(JobCompanySearcher searcher)
        {
            WebCommon.LoadLevelDropDownList(ddlHomeAddr,ddlHomeAddrSub,HomeAddressRlist,
                Constants.GetParentCodeBySubCode(searcher.HomeAddr),searcher.HomeAddr,
                Constants.DictionaryType.ADDR_CITY);
            WebCommon.LoadLevelDropDownList(ddlResideAddr,ddlResideAddrSub,ResideAddressRList,
                Constants.GetParentCodeBySubCode(searcher.ResideAddr),searcher.ResideAddr,
                Constants.DictionaryType.ADDR_CITY);
            ddlRegistDiff.SelectedValue = 
                (searcher.LastCreateDiff.HasValue) ? searcher.LastCreateDiff.ToString() : "0";
            ddlLoginDiff.SelectedValue = 
                (searcher.LastLoginDiff.HasValue) ? searcher.LastLoginDiff.ToString() : "0";
            ddlRefreshDiff.SelectedValue = 
                (searcher.LastRefreshDiff.HasValue) ? searcher.LastRefreshDiff.ToString() : "0";
            string sex = (null == searcher.PersonSex) ? "" : searcher.PersonSex.Trim();
            ddlSexy.SelectedValue = sex;
            
            ddlAgeMax.SelectedValue = (searcher.AgeMax.HasValue) ? searcher.AgeMax.ToString() : "0";
            ddlAgeMin.SelectedValue = (searcher.AgeMin.HasValue) ? searcher.AgeMin.ToString() : "0";
            
            ddlGradeMax.SelectedValue = (searcher.GradYearMax.HasValue) ? searcher.GradYearMax.ToString() : "0";
            ddlGradeMin.SelectedValue = (searcher.GradYearMin.HasValue) ? searcher.GradYearMin.ToString() : "0";

            ddlWorkMax.SelectedValue = (searcher.WorkLenMax.HasValue) ? searcher.WorkLenMax.ToString() : "0";
            ddlWorkMin.SelectedValue = (searcher.WorkLenMin.HasValue) ? searcher.WorkLenMin.ToString() : "0";

            string eduMin = string.Empty;
            if ( searcher.EduMin.HasValue) eduMin = searcher.EduMin.ToString();
            WebCommon.SetDropDownFromDict(ddlEduMin,eduMin,
                Constants.GetDictionaryCode(Constants.DictionaryType.EDUCATION_LEVEL),true);

            string eduMax = string.Empty;
            if ( searcher.EduMax.HasValue) eduMax = searcher.EduMax.ToString();
            WebCommon.SetDropDownFromDict(ddlEduMax,eduMax,
                Constants.GetDictionaryCode(Constants.DictionaryType.EDUCATION_LEVEL),true);

            WebCommon.LoadLevelDropDownList(ddlSpec,ddlSpecSub,RelatedListSpec,
                Constants.GetParentCodeBySubCode(searcher.SpecialtyClass),searcher.SpecialtyClass,
                Constants.DictionaryType.SPECIALTY_CLASS);      

            WebCommon.LoadLevelDropDownList(ddlIndustry,ddlIndustrySub,RelatedListIndustry,
                Constants.GetParentCodeBySubCode(searcher.CurIndustry),searcher.CurIndustry,
                Constants.DictionaryType.INDUSTRY_CLASS);    

            string lang = string.Empty;
            if ( searcher.ForeignLang.HasValue) lang = searcher.ForeignLang.ToString();
            WebCommon.SetDropDownFromDict(ddlLanguagetype,lang,
                Constants.GetDictionaryCode(Constants.DictionaryType.FOREIGN_LANG),true);

            string langSkill = string.Empty;
            if ( searcher.ForeignSkill.HasValue) langSkill = searcher.ForeignSkill.ToString();
            WebCommon.SetDropDownFromDict( dlllanguageSkill,langSkill,
                Constants.GetDictionaryCode(Constants.DictionaryType.LANG_SKILL),true);

            string computerSkill = string.Empty;
            if (searcher.ComputerSkill.HasValue) computerSkill = searcher.ComputerSkill.ToString();
            WebCommon.SetDropDownFromDict(ddlComputerLevel,computerSkill,
                Constants.GetDictionaryCode(Constants.DictionaryType.LANG_SKILL),true);

            string dialectSkill = string.Empty;
            if ( searcher.DialectLang.HasValue) dialectSkill = searcher.DialectLang.ToString();
            WebCommon.SetDropDownFromDict(ddlLanYueSKill,dialectSkill,
                Constants.GetDictionaryCode(Constants.DictionaryType.LANG_SKILL),true);

            string techTitle = string.Empty;
            if ( searcher.TechTitle.HasValue) techTitle = searcher.TechTitle.ToString();
            WebCommon.SetDropDownFromDict(ddlTechTitle,techTitle,
                Constants.GetDictionaryCode(Constants.DictionaryType.TECH_TITLE),true);

            string curState = string.Empty;
            if ( searcher.JobStatus.HasValue ) curState = searcher.JobStatus.ToString();
            WebCommon.SetDropDownFromDict(ddlCurJobState,curState,
                Constants.GetDictionaryCode(Constants.DictionaryType.CURRENT_WORKSTATUS),true);

            string applyStyle = string.Empty;
            if ( searcher.ApplyStyle.HasValue) applyStyle = searcher.ApplyStyle.ToString();
            WebCommon.SetDropDownFromDict(ddlApplyStyle,applyStyle,
                Constants.GetDictionaryCode(Constants.DictionaryType.APPLY_TYPE),true);

            ddlResumeState.SelectedValue = 
                (searcher.ResumeState.HasValue) ? searcher.ResumeState.ToString() : "1";

            ddlPersonSenior.SelectedValue = 
                (searcher.IsSenior.HasValue ) ? searcher.IsSenior.ToString() : "0";

            string personClass = string.Empty;
            if ( searcher.PersonClass.HasValue ) personClass = searcher.PersonClass.ToString();
            WebCommon.SetDropDownFromDict(ddlPersonClass,personClass,
                Constants.GetDictionaryCode(Constants.DictionaryType.PERSON_CLASS),true);

            txtLoginCode.Text = searcher.PersonLoginName;
            txtPersonName.Text = searcher.PersonName;
            txtSearcherName.Text = searcher.SearcherName;
            txtKey.Text = searcher.SearchKey;
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                JobCompanySearcher searcher = GetSearcher();

                SearcherLogic logic = new SearcherLogic();
                if (SearcherId > 0)
                {
                    searcher.SearcherId = SearcherId;
                    logic.UpdateSearcher(searcher);
                }
                else
                {
                    logic.InsertSearcher(searcher);
                }

                MySession session = new MySession();
                if (null != session.GetAdminUserFromSession())
                {
                    string url = WebCommon.GetApplicationPath(Request)
                        + "AdminWeb/AdminMain.aspx?controlType="
                        + ((int)Constants.SubjectType.CompanySearcher).ToString();
                    Response.Redirect(url);
                    return;
                }
                else
                {
                }
            }
            catch (Exception ex)
            {
                ShowMessage("保存搜索器失败：" + ex.Message);
            }
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            try
            {
                JobCompanySearcher seacher = GetSearcher();
                String key = "C" + DateTime.Today.Ticks.ToString();
                Cache[key] = seacher;

                MySession session = new MySession();
                string url = "";

                if (null != session.GetAdminUserFromSession())
                {
                    url = WebCommon.GetApplicationPath(Request)
                        + "AdminWeb/AdminMain.aspx?controlType="
                        + ((int)Constants.SubjectType.CompanySearcher).ToString()
                        + "&type=1&key=" + key;
                }
                else
                {
                }
                Response.Redirect(url);                
            }
            catch (Exception ex)
            {
                Logger.WriteLog(ex.StackTrace);
                ShowMessage("内部异常，无法查询求职者：" + ex.Message);
            }
        }

        private JobCompanySearcher GetSearcher()
        {
            JobCompanySearcher searcher = new JobCompanySearcher();
            try
            {
                if (ddlHomeAddrSub.SelectedValue.Length > 1)
                    searcher.HomeAddr = ddlHomeAddrSub.SelectedValue;
                else if (ddlHomeAddr.SelectedValue.Length > 1)
                    searcher.HomeAddr = ddlHomeAddr.SelectedValue;

                if (ddlResideAddrSub.SelectedValue.Length > 1)
                    searcher.ResideAddr = ddlResideAddrSub.SelectedValue;
                else if (ddlResideAddr.SelectedValue.Length > 1)
                    searcher.ResideAddr = ddlResideAddr.SelectedValue;

                searcher.LastLoginDiff = Convert.ToInt32(ddlLoginDiff.SelectedValue);
                searcher.LastRefreshDiff = Convert.ToInt32(ddlRefreshDiff.SelectedValue);
                searcher.LastCreateDiff = Convert.ToInt32(ddlRegistDiff.SelectedValue);
                searcher.PersonSex = ddlSexy.SelectedValue;

                searcher.AgeMin = Convert.ToInt32(ddlAgeMin.SelectedValue);
                searcher.AgeMax = Convert.ToInt32(ddlAgeMax.SelectedValue);
                searcher.GradYearMin = Convert.ToInt32(ddlGradeMin.SelectedValue);
                searcher.GradYearMax = Convert.ToInt32(ddlGradeMax.SelectedValue);
                searcher.WorkLenMin = Convert.ToInt32(ddlWorkMin.SelectedValue);
                searcher.WorkLenMax = Convert.ToInt32(ddlWorkMax.SelectedValue);

                searcher.EduMin = Convert.ToInt32(ddlEduMin.SelectedValue);
                searcher.EduMax = Convert.ToInt32(ddlEduMax.SelectedValue);

                if (ddlSpecSub.SelectedValue.Length > 1)
                    searcher.SpecialtyClass = ddlSpecSub.SelectedValue;
                else if (ddlSpec.SelectedValue.Length > 1)
                    searcher.SpecialtyClass = ddlSpec.SelectedValue;

                if (ddlIndustrySub.SelectedValue.Length > 1)
                    searcher.CurIndustry = ddlIndustrySub.SelectedValue;
                else if (ddlIndustry.SelectedValue.Length > 1)
                    searcher.CurIndustry = ddlIndustry.SelectedValue;

                searcher.ForeignLang = Convert.ToInt32(ddlLanguagetype.SelectedValue);
                searcher.ForeignSkill = Convert.ToInt32(dlllanguageSkill.SelectedValue);
                searcher.ComputerSkill = Convert.ToInt32(ddlComputerLevel.SelectedValue);
                searcher.DialectLang = Convert.ToInt32(ddlLanYueSKill.SelectedValue);
                searcher.TechTitle = Convert.ToInt32(ddlTechTitle.SelectedValue);
                searcher.JobStatus = Convert.ToInt32(ddlCurJobState.SelectedValue);
                searcher.ApplyStyle = Convert.ToInt32(ddlApplyStyle.SelectedValue);

                if (ddlResumeState.Visible)
                    searcher.ResumeState = Convert.ToInt32(ddlResumeState.SelectedValue);
                else
                    searcher.ResumeState = 1;

                if (ddlPersonSenior.Visible)
                    searcher.IsSenior = Convert.ToInt32(ddlPersonSenior.SelectedValue);
                else
                    searcher.IsSenior = 0;

                if (ddlPersonClass.Visible)
                    searcher.PersonClass = Convert.ToInt32(ddlPersonClass.SelectedValue);
                else
                    searcher.PersonClass = 0;

                if (txtLoginCode.Visible)
                    searcher.PersonLoginName = txtLoginCode.Text.Trim();
                else
                    searcher.PersonLoginName = "";

                searcher.PersonName = txtPersonName.Text.Trim();
                searcher.SearcherName = txtSearcherName.Text.Trim();

                searcher.SearchKey = txtKey.Text.Trim();

                MySession session = new MySession();
                searcher.CreatorCompany = session.GetCompanyFromSession().CompanyId;
                searcher.CreatorLogincode = session.GetCompanyFromSession().LoginCode;
            }
            catch (Exception e)
            {
                Logger.WriteLog(e.Message);
            }
            return searcher;
        }

    }
}